﻿import QtQuick 2.0

Item {

    id: root

    property var model: ["1", "2", "3"]

    property int checkedIndex: -1
    property string text: checkedIndex===-1 ? "" : model[checkedIndex]

    property bool editable: true

    property var spacing: 10*dp
    property var rows: 1

    property string title: ""
    property real titleWidth: title==="" ? 0 : 80*dp
    property int titleHeight: title==="" ? 0 : 40*dp
    property var titleHAlign: Text.AlignRight       // 标题水平对齐方式
    property var titleVAlign: Text.AlignVCenter     // 标题垂直对齐方式
    property bool titleAlignJustify: true           // 标题是否两端对齐（设置为true时会覆盖titleHAline属性！！！）
    property color titleColor: "#000"
    property bool required: false

    signal cliccc(int checkedIndex);


    property color radio_background_color: "transparent"
    property color radio_background_color_select: "transparent"
    property color radio_background_border: "transparent"
    property color radio_background_border_select: "transparent"

    property bool defaultMode1: false

    height: 40*dp; width: layout_grid.implicitWidth

    Component.onCompleted: {
        if(defaultMode1){

            radio_background_color = "transparent"
            radio_background_color_select = Qt.binding(function(){return config.getOpacityColor(config.themeColor, 0.1)})
            radio_background_border = "#DDDDDD"
            radio_background_border_select = Qt.binding(function(){return config.themeColor})
        }
    }
    // 标题(选填)
    Item{
        id: rectTitle
        height: titleHeight
        width: titleWidth - 16*dp       // titleWidth比用户设置的titleWidth短一点
        Text{
            id: text_title
            text: {
                if(!titleAlignJustify) return title
                else{

                    if(text_rawTitle.contentWidth === 0 || text_space.contentWidth === 0){
                        return   title
                    }

                    return funcTools.justifyAlignAppendSpace(title, parent.width, text_rawTitle.width, text_space.width)
                }
            }
            font.pixelSize: config.fontsize
            anchors.fill: parent
            color: titleColor
            verticalAlignment: titleVAlign
            horizontalAlignment: titleAlignJustify ? Text.AlignHCenter : titleHAlign
        }
        Text{           // 固定在左边的星号
            visible: required
            text: '<font color="#FF0000"">* </font>'
            x: -10*dp
            font.pixelSize: config.fontsize
            height: titleHeight
            width: 10*dp
            verticalAlignment: Text.AlignVCenter
        }
    }
    Text{           // 用来计算空格的长度
        id: text_space
        text: " "
        font.pixelSize: config.fontsize
        visible: false
    }
    Text{           // 用来计算纯文本的长度
        id: text_rawTitle
        text: title
        font.pixelSize: config.fontsize
        visible: false
    }

    Grid{
        id: layout_grid
        x: titleWidth
        rows: 1
        spacing: root.spacing

        Repeater{
            model: root.model.length
            CustomRadio{

                editable: root.editable
                height: root.height
                title: root.model[index]
                checked: checkedIndex === index
                onClicked: {
                    checkedIndex = index
                    cliccc(checkedIndex)
                }

                // 样式

                background_color: radio_background_color
                background_color_select: radio_background_color_select
                background_border: radio_background_border
                background_border_select: radio_background_border_select

            }
        }
    }

}
